/*
 * Copyright [2011] [Ambud Sharma/ODep Monitor]

   Licensed under the Apache License, Version 2.0 (the "License");

   you may not use this file except in compliance with the License.

   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software

   distributed under the License is distributed on an "AS IS" BASIS,

   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   See the License for the specific language governing permissions and

   limitations under the License.
 */
package edu.uwsp.orion.odepm.main.server;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.server.namenode.DatanodeDescriptor;
import org.apache.hadoop.mapred.JobStatus;

import edu.uwsp.orion.odepm.main.client.DashboardInitializerService;
import edu.uwsp.orion.odepm.main.shared.ClusterStats;
import edu.uwsp.orion.odepm.main.shared.ClusterTopology;
import edu.uwsp.orion.odepm.main.shared.DashboardMain;
import edu.uwsp.orion.odepm.main.shared.JobSummary;
import edu.uwsp.orion.odepm.main.shared.NamenodeInfo;
import edu.uwsp.orion.odepm.main.shared.NamenodeWebInfo;
import edu.uwsp.orion.odepm.main.shared.Node;
import edu.uwsp.orion.odepm.main.shared.Rack;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class DashboardInitializerServiceImpl extends RemoteServiceServlet implements DashboardInitializerService {

	HadoopUtils utils;
	@Override
	public DashboardMain getInfo() {
		utils=new HadoopUtils();
		DashboardMain dashboardMain=new DashboardMain();				
		dashboardMain.nameNode=getNamenodeInfo();
		//dashboardMain.stats=getClusterStats();
		
		return dashboardMain;
	}
	
	private NamenodeWebInfo getNamenodeInfo(){
		NamenodeWebInfo info=new NamenodeWebInfo();
		info.address=utils.nameNode.address;
		info.core_count=utils.cpuCount;
		info.disk_size=utils.nameNode.disk_size;
		info.gflops=utils.nameNode.gflops;
		info.heapSize=utils.memory+"";
		info.node_count=utils.nameNode.node_count;
		info.port=utils.nameNode.port;		
		return info;
	}
	
	private ClusterStats getClusterStats(){
		ClusterStats clusterStats=new ClusterStats();
		clusterStats.cpu=utils.CPU;
		clusterStats.memory=utils.memory;
		
		return clusterStats;
	}
	
	private ClusterTopology getClusterTopology(){
		ClusterTopology clusterTopology=new ClusterTopology();
		return clusterTopology;
	}
	
	private JobSummary jobSummary(){
		JobSummary jobSummary=new JobSummary();
		return jobSummary;
	}

	@Override
	public ArrayList<JobSummary> getJobs() {
		ArrayList<JobSummary> jobSummaries=new ArrayList<JobSummary>();
		try {
			JobStatus[] jobs = utils.jobClient.getAllJobs();
			for(JobStatus stats:jobs){
				JobSummary summary=new JobSummary();
				summary.jobID=stats.getJobId();
				summary.mapProgress=stats.mapProgress();
				summary.reduceProgress=stats.reduceProgress();
				summary.jobName=stats.getJobID().getJtIdentifier();
				jobSummaries.add(summary);
			}
		} catch (IOException e) {			
			e.printStackTrace();
		}
		return jobSummaries;
	}

	//@Override
	public JobStatus getJobStatus(String jobid) {
		try {
			JobStatus[] jobs = utils.jobClient.getAllJobs();
			for(JobStatus job:jobs){
				if(job.getJobId().equals(jobid)){
					return job;
				}
			}
		} catch (IOException e) {			
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ArrayList<Rack> getTopology() {
		ArrayList<Rack> racks=new ArrayList<Rack>();
		try{
			Rack rack=new Rack();
			rack.rackName=utils.datanaodesLive.get(0).getNetworkLocation();
			for(DatanodeDescriptor desc:utils.datanaodesLive){
				Node node=new Node();
				node.nodeName=desc.getHostName();
				node.status=true;
				rack.nodes.add(node);
				racks.add(rack);		
			}
		}
		catch(Exception e){
			System.err.print("sorry couldn't get topology");
		}
		return racks;
	}
}
